home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 114 / PC Guia 114.iso / Software / Utils / The Gimp 2.2.1 / gimp-help-2-0.6-setup.exe / {app} / share / gimp / 2.0 / help / en / ch02s10s03.html < prev    next >
Encoding:
Extensible Markup Language  |  2004-12-19  |  13.9 KB  |  401 lines

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.   <head>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6.     <title>10.3.┬áLists, Lists And More Lists</title>
  7.     <link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
  8.     <link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
  9.     <meta name="generator" content="DocBook XSL Stylesheets V1.66.1" />
  10.     <link rel="start" href="index.html" title="GIMP User Manual" />
  11.     <link rel="up" href="ch02s10.html" title="10.┬áA Script-Fu Tutorial" />
  12.     <link rel="prev" href="ch02s10s02.html" title="10.2.┬áVariables And Functions" />
  13.     <link rel="next" href="ch02s10s04.html" title="10.4.┬áYour First Script-Fu Script" />
  14.   </head>
  15.   <body>
  16.     <div xmlns="" class="navheader">
  17.       <table width="100%" summary="Navigation header">
  18.         <tr>
  19.           <th colspan="3" align="center" id="chaptername">10.┬áA Script-Fu Tutorial</th>
  20.         </tr>
  21.         <tr>
  22.           <td width="20%" align="left"><a accesskey="p" href="ch02s10s02.html">Prev</a>┬á</td>
  23.           <th width="60%" align="center" id="sectionname">10.3.┬áLists, Lists And More Lists</th>
  24.           <td width="20%" align="right">┬á<a accesskey="n" href="ch02s10s04.html">Next</a></td>
  25.         </tr>
  26.       </table>
  27.       <hr />
  28.     </div>
  29.     <div class="sect2" lang="en" xml:lang="en">
  30.       <div class="titlepage">
  31.         <div>
  32.           <div>
  33.             <h3 class="title"><a id="id3429666"></a>10.3.┬áLists, Lists And More Lists</h3>
  34.           </div>
  35.         </div>
  36.       </div>
  37.       <p>
  38.       We've trained you in variables and functions, and now enter the
  39.       murky swamps of Scheme's lists. 
  40.     </p>
  41.       <div class="simplesect" lang="en" xml:lang="en">
  42.         <div class="titlepage">
  43.           <div>
  44.             <div>
  45.               <h4 class="title"><a id="id3429679"></a>Defining A List</h4>
  46.             </div>
  47.           </div>
  48.         </div>
  49.         <p>
  50.         Before we talk more about lists, it is necessary that you know
  51.         the difference between atomic values and lists.  
  52.       </p>
  53.         <p>
  54.         You've already seen atomic values when we initialized
  55.         variables in the previous lesson. An atomic value is a single
  56.         value. So, for example, we can assign the variable "x" the
  57.         single value of 8 in the following statement: 
  58.       </p>
  59.         <pre class="programlisting">
  60.         (let* ( (x 8) ) x)
  61.       </pre>
  62.         <p>
  63.         (We added the expression <tt class="varname">x</tt> at the end to print out the value
  64.         assigned to <tt class="varname">x</tt>-- normally you won't need to do this. Notice how
  65.         <tt class="code">let*</tt> operates just like a function: The value of
  66.         the last statement is the value returned.) 
  67.       </p>
  68.         <p>
  69.         A variable may also refer to a list of values, rather than a
  70.         single value. To assign the variable <tt class="varname">x</tt> the
  71.         list of values 1, 3, 5, we'd type: 
  72.       </p>
  73.         <pre class="programlisting">
  74.         (let* ( (x '(1 3 5))) x)
  75.       </pre>
  76.         <p>
  77.         Try typing both statements into the Script-Fu Console and
  78.         notice how it replies. When you type the first statement in,
  79.         it simply replies with the result: 
  80.       </p>
  81.         <pre class="programlisting">
  82.         8
  83.       </pre>
  84.         <p>
  85.         However, when you type in the other statement, it replies with
  86.         the following result: 
  87.       </p>
  88.         <pre class="programlisting">
  89.         (1 3 5)
  90.       </pre>
  91.         <p>
  92.          When it replies with the value 8 it is informing you that
  93.          <tt class="varname">x</tt> contains the atomic value 8. However,
  94.          when it replies with (1 3 5), it is then informing you that
  95.          <tt class="varname">x</tt> contains not a single value, but a list
  96.          of values. Notice that there are no commas in our declaration
  97.          or assignment of the list, nor in the printed result. 
  98.       </p>
  99.         <p>
  100.          The syntax to define a list is:
  101.       </p>
  102.         <pre class="programlisting">
  103.          '(a b c)
  104.       </pre>
  105.         <p>
  106.         where <tt class="varname">a</tt>, <tt class="varname">b</tt>, and
  107.         <tt class="varname">c</tt> are literals. We use the apostrophe (')
  108.         to indicate that what follows in the parentheses is a list of
  109.         literal values, rather than a function or expression. 
  110.       </p>
  111.         <p>
  112.         An empty list can be defined as such:
  113.       </p>
  114.         <pre class="programlisting">
  115.         '()
  116.       </pre>
  117.         <p>
  118.         or simply:
  119.       </p>
  120.         <pre class="programlisting">
  121.         ()
  122.       </pre>
  123.         <p>
  124.         Lists can contain atomic values, as well as other lists:
  125.       </p>
  126.         <pre class="programlisting">
  127.         (let*
  128.            (
  129.                 (x 
  130.                    '("The Gimp" (1 2 3) ("is" ("great" () ) ) )
  131.                 )
  132.             )       
  133.                 
  134.             x
  135.          )
  136.       </pre>
  137.         <p>
  138.         Notice that after the first apostrophe, you no longer need to
  139.         use an apostrophe when defining the inner lists. Go ahead and
  140.         copy the statement into the Script-Fu Console and see what it
  141.         returns. 
  142.       </p>
  143.         <p>
  144.         You should notice that the result returned is not a list of
  145.         single, atomic values; rather, it is a list of a literal <tt class="code">("The
  146.         Gimp")</tt>, the list <tt class="code">(1 2 3)</tt>, etc.
  147.       </p>
  148.       </div>
  149.       <div class="simplesect" lang="en" xml:lang="en">
  150.         <div class="titlepage">
  151.           <div>
  152.             <div>
  153.               <h4 class="title"><a id="id3429854"></a>How To Think Of Lists</h4>
  154.             </div>
  155.           </div>
  156.         </div>
  157.         <p>
  158.         It's useful to think of lists as composed of a "head" and a
  159.         "tail." The head is the first element of the list, the tail
  160.         the rest of the list. You'll see why this is important when we
  161.         discuss how to add to lists and how to access elements in the
  162.         list. 
  163.       </p>
  164.       </div>
  165.       <div class="simplesect" lang="en" xml:lang="en">
  166.         <div class="titlepage">
  167.           <div>
  168.             <div>
  169.               <h4 class="title"><a id="id3429871"></a>Creating Lists Through Concatenation (The Cons Function)</h4>
  170.             </div>
  171.           </div>
  172.         </div>
  173.         <p>
  174.         One of the more common functions you'll encounter is the cons
  175.         function. It takes a value and prepends it to its second
  176.         argument, a list. From the previous section, I suggested that
  177.         you think of a list as being composed of an element (the head)
  178.         and the remainder of the list (the tail). This is exactly how
  179.         cons functions -- it adds an element to the head of a
  180.         list. Thus, you could create a list as follows: 
  181.       </p>
  182.         <pre class="programlisting">
  183.         (cons 1 '(2 3 4) )
  184.       </pre>
  185.         <p>
  186.         The result is the list <tt class="code">(1 2 3 4)</tt>.
  187.       </p>
  188.         <p>
  189.         You could also create a list with one element:
  190.       </p>
  191.         <pre class="programlisting">
  192.         (cons 1 () )
  193.       </pre>
  194.         <p>
  195.         You can use previously declared variables in place of any
  196.         literals, as you would expect. 
  197.       </p>
  198.       </div>
  199.       <div class="simplesect" lang="en" xml:lang="en">
  200.         <div class="titlepage">
  201.           <div>
  202.             <div>
  203.               <h4 class="title"><a id="id3429918"></a>Defining A List Using The list Function</h4>
  204.             </div>
  205.           </div>
  206.         </div>
  207.         <p>
  208.         To define a list composed of literals or previously declared
  209.         variables, use the list function: 
  210.       </p>
  211.         <pre class="programlisting">
  212.         (list 5 4 3 a b c)
  213.       </pre>
  214.         <p>
  215.         This will compose and return a list containing the values held
  216.         by the variables <tt class="varname">a</tt>, <tt class="varname">b</tt>
  217.         and <tt class="varname">c</tt>. For example:  
  218.       </p>
  219.         <pre class="programlisting">
  220.         (let*  (
  221.                   (a 1)
  222.                   (b 2)
  223.                   (c 3)
  224.                )
  225.                (list 5 4 3 a b c)
  226.         )
  227.       </pre>
  228.         <p>
  229.         This code creates the list <tt class="code">(5 4 3 1 2 3)</tt>.
  230.       </p>
  231.       </div>
  232.       <div class="simplesect" lang="en" xml:lang="en">
  233.         <div class="titlepage">
  234.           <div>
  235.             <div>
  236.               <h4 class="title"><a id="id3429970"></a>Accessing Values In A List</h4>
  237.             </div>
  238.           </div>
  239.         </div>
  240.         <p>
  241.         To access the values in a list, use the functions <tt class="code">car</tt> and <tt class="code">cdr</tt>,
  242.         which return the first element of the list and the rest of the
  243.         list, respectively. These functions break the list down into
  244.         the head::tail construct I mentioned earlier. 
  245.       </p>
  246.       </div>
  247.       <div class="simplesect" lang="en" xml:lang="en">
  248.         <div class="titlepage">
  249.           <div>
  250.             <div>
  251.               <h4 class="title"><a id="id3429994"></a>The <tt class="code">car</tt> Function</h4>
  252.             </div>
  253.           </div>
  254.         </div>
  255.         <p>
  256.         <tt class="code">car</tt> returns the first element of the list (the
  257.         head of the list). The list needs to be non-null. Thus, the
  258.         following returns the first element of the list: 
  259.       </p>
  260.         <pre class="programlisting">
  261.         (car '("first" 2 "third"))
  262.       </pre>
  263.         <p>
  264.         which is:
  265.       </p>
  266.         <pre class="programlisting">
  267.         "first"
  268.       </pre>
  269.       </div>
  270.       <div class="simplesect" lang="en" xml:lang="en">
  271.         <div class="titlepage">
  272.           <div>
  273.             <div>
  274.               <h4 class="title"><a id="id3430031"></a>The <tt class="code">cdr</tt> function</h4>
  275.             </div>
  276.           </div>
  277.         </div>
  278.         <p>
  279.         <tt class="code">cdr</tt> returns the rest of the list after the first
  280.         element (the tail of the list). If there is only one element
  281.         in the list, it returns an empty list. 
  282.       </p>
  283.         <pre class="programlisting">
  284.         (cdr '("first" 2 "third"))
  285.       </pre>
  286.         <p>
  287.         returns:
  288.       </p>
  289.         <pre class="programlisting">
  290.         (2 "third")
  291.       </pre>
  292.         <p>
  293.         whereas the following:
  294.       </p>
  295.         <pre class="programlisting">
  296.         (cdr '("one and only"))
  297.       </pre>
  298.         <p>
  299.         returns:
  300.       </p>
  301.         <pre class="programlisting">
  302.         ()
  303.       </pre>
  304.       </div>
  305.       <div class="simplesect" lang="en" xml:lang="en">
  306.         <div class="titlepage">
  307.           <div>
  308.             <div>
  309.               <h4 class="title"><a id="id3430086"></a>Accessing Other Elements In A List</h4>
  310.             </div>
  311.           </div>
  312.         </div>
  313.         <p>
  314.         OK, great, we can get the first element in a list, as well as
  315.         the rest of the list, but how do we access the second, third
  316.         or other elements of a list? There exist several "convenience"
  317.         functions to access, for example, the head of the head of the
  318.         tail of a list (<tt class="code">caadr</tt>), the tail of the tail of a
  319.         list (<tt class="code">cddr</tt>), etc. 
  320.       </p>
  321.         <p>
  322.         The basic naming convention is easy: The a's and d's represent
  323.         the heads and tails of lists, so 
  324.       </p>
  325.         <pre class="programlisting">
  326.         (car (cdr (car x) ) )
  327.       </pre>
  328.         <p>
  329.         could be written as:
  330.       </p>
  331.         <pre class="programlisting">
  332.         (cadar x)
  333.       </pre>
  334.         <p>
  335.         To view a full list of the list functions, refer to the
  336.         Appendix, which lists the available functions for the version
  337.         of Scheme used by Script-Fu. 
  338.       </p>
  339.         <p>
  340.         To get some practice with list-accessing functions, try typing
  341.         in the following (except all on one line if you're using the
  342.         console); use different variations of car and cdr to access
  343.         the different elements of the list: 
  344.       </p>
  345.         <pre class="programlisting">
  346.         (let* (
  347.                  (x  '( (1 2 (3 4 5) 6)  7  8  (9 10) )
  348.                  )
  349.               )
  350.               ; place your car/cdr code here
  351.         )
  352.       </pre>
  353.         <p>
  354.         Try accessing the number 3 in the list using only two function
  355.         calls. If you can do that, you're on your way to becoming a
  356.         Script-Fu Master! 
  357.       </p>
  358.         <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
  359.           <table border="0" summary="Note">
  360.             <tr>
  361.               <td rowspan="2" align="center" valign="top" width="25">
  362.                 <img alt="[Note]" src="../images/note.png" />
  363.               </td>
  364.               <th align="left">Note</th>
  365.             </tr>
  366.             <tr>
  367.               <td colspan="2" align="left" valign="top">
  368.                 <p>
  369.       In Scheme, a semicolon (";") marks a comment.  It, and
  370.       anything that follows it on the same line, are ignored by the
  371.       script interpreter, so you can use this to add comments to jog
  372.       your memory when you look at the script later.
  373.     </p>
  374.               </td>
  375.             </tr>
  376.           </table>
  377.         </div>
  378.       </div>
  379.     </div>
  380.     <div class="navfooter">
  381.       <hr />
  382.       <table width="100%" summary="Navigation footer">
  383.         <tr>
  384.           <td width="40%" align="left"><a accesskey="p" href="ch02s10s02.html">Prev</a>┬á</td>
  385.           <td width="20%" align="center">
  386.             <a accesskey="u" href="ch02s10.html">Up</a>
  387.           </td>
  388.           <td width="40%" align="right">┬á<a accesskey="n" href="ch02s10s04.html">Next</a></td>
  389.         </tr>
  390.         <tr>
  391.           <td width="40%" align="left" valign="top">10.2.┬áVariables And Functions┬á</td>
  392.           <td width="20%" align="center">
  393.             <a accesskey="h" href="index.html">Home</a>
  394.           </td>
  395.           <td width="40%" align="right" valign="top">┬á10.4.┬áYour First Script-Fu Script</td>
  396.         </tr>
  397.       </table>
  398.     </div>
  399.   </body>
  400. </html>
  401.